package realtime.statistic;

import entity.realtime.TripIndex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utility.database.SqlServerAccess;
import utility.time.ProcessTime;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lit
 * @date 2018/4/9.
 */
public class ProcessTripIndex {
    /**
     * 数据连接
     */
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private SqlServerAccess mssql = null;

    /**
     * 用于输出信息
     */
    private Logger logger = null;

    /**
     * 统计年月
     */
    private int year;
    private int month;

    private boolean initMSSQL(){
        if( mssql == null ){
            try {
                mssql = new SqlServerAccess();
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        if( connection == null ){
            try {
                connection = mssql.getInstance();
                connection.setAutoCommit(false);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private void finishMSSQL(){
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public void processTripIndex(String lastStatMonth, String statTime){
        logger = LoggerFactory.getLogger(ProcessTripIndex.class);

        year = ProcessTime.getYear(lastStatMonth);
        month = ProcessTime.getMonth(lastStatMonth);

        logger.info("从TripIndex数据表中获取待处理数据");
        Map<String,Map<String,TripIndex>> pendingTripIndex = getPendingData(statTime);
        //遍历时间
        for(String statDate : pendingTripIndex.keySet()){
            logger.info("处理时间{}的五分钟数据",statDate);
            Map<String,TripIndex> tripIndexAreaMap = pendingTripIndex.get(statDate);
            logger.debug("获取时间{}的五分钟点火车辆数",statDate);
            getFireNum(tripIndexAreaMap,statDate);

            logger.debug("获取时间{}的五分钟点火扩样车辆数",statDate);
            getFireNumExpansion(tripIndexAreaMap,statDate);

            logger.debug("获取时间{}的累计点火车辆数",statDate);
            getSumFireNum(tripIndexAreaMap,statDate);

            logger.debug("获取时间{}的累计点火扩样车辆数",statDate);
            getSumFireNumExpansion(tripIndexAreaMap,statDate);

            logger.debug("获取时间{}的其他剩余字段值",statDate);
            calculateOtherField(tripIndexAreaMap);
        }
        if(pendingTripIndex.size() > 0){
            logger.info("更新tripIndex数据表");
            updatePendingTripIndex(pendingTripIndex);
            logger.info("更新结束");
            updateLatestTime();
        }else{
            logger.info("无待处理数据");
        }
        finishMSSQL();
    }

    /**
     * 将最后处理的行程日期记录下来
     */
    private void updateLatestTime() {
        if(initMSSQL()){
            String sql = "update a set a.statYear=year(b.c),a.statMonth=month(b.c),a.statDay=day(b.c),a.statTime=b.c from IDBox_Master.dbo.stat_lasttime a,(select top 1 StatDate c from rtm_tripIndex where status= 1 and areaId=1000 order by statDate desc) b";
            try{
                preparedStatement  = connection.prepareStatement(sql);
                int num = preparedStatement.executeUpdate();
                connection.commit();
                if(num > 0){
                    logger.info("最近行程更新时间已存入数据库");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 7.更新tripIndex数据表
     * @param pendingTripIndex
     */
    private void updatePendingTripIndex(Map<String, Map<String, TripIndex>> pendingTripIndex) {
        if(initMSSQL()){
            String sql = "UPDATE rtm_tripIndex SET FireNum=?,SumFireNum=?,FireNumExpansion=?,SumFireNumExpansion=?,UnfireNum=?,OfflineNum=?,FireIndex=?,PressIndex=?,OutRate=?,UnfireNumExpansion=?,OfflineNumExpansion=?,FireIndexExpansion=?,PressIndexExpansion=?,OutRateExpansion=?,Status=1 WHERE ID=?";
            try{
                preparedStatement = connection.prepareStatement(sql);
                int i=0;
                for(String statDate : pendingTripIndex.keySet()){
                    Map<String,TripIndex> tripIndexAreaMap = pendingTripIndex.get(statDate);
                    for(String areaID : tripIndexAreaMap.keySet()){
                        TripIndex tripIndex = tripIndexAreaMap.get(areaID);
                        if(tripIndex.getFireNum() != null){
                            preparedStatement.setInt(1,tripIndex.getFireNum());
                        }else{
                            preparedStatement.setNull(1, Types.INTEGER);
                        }
                        if(tripIndex.getSumFireNum() != null){
                            preparedStatement.setInt(2,tripIndex.getSumFireNum());
                        }else{
                            preparedStatement.setNull(2,Types.INTEGER);
                        }
                        if(tripIndex.getFireNumExpansion() != null){
                            preparedStatement.setInt(3,tripIndex.getFireNumExpansion());
                        }else{
                            preparedStatement.setNull(3,Types.INTEGER);
                        }
                        if(tripIndex.getSumFireNumExpansion() != null){
                            preparedStatement.setInt(4,tripIndex.getSumFireNumExpansion());
                        }else{
                            preparedStatement.setNull(4,Types.INTEGER);
                        }
                        //其他字段在第6步计算时已经给了初值，所以可以不用判断是否为空
                        preparedStatement.setInt(5,tripIndex.getUnfireNum());
                        preparedStatement.setInt(6,tripIndex.getOfflineNum());
                        preparedStatement.setDouble(7,tripIndex.getFireIndex());
                        preparedStatement.setDouble(8,tripIndex.getPressIndex());
                        preparedStatement.setDouble(9,tripIndex.getOutRate());
                        preparedStatement.setInt(10,tripIndex.getUnfireNumExpansion());
                        preparedStatement.setInt(11,tripIndex.getOfflineNumExpansion());
                        preparedStatement.setDouble(12,tripIndex.getFireIndexExpansion());
                        preparedStatement.setDouble(13,tripIndex.getPressIndexExpansion());
                        preparedStatement.setDouble(14,tripIndex.getOutRateExpansion());
                        preparedStatement.setInt(15,tripIndex.getId());
                        preparedStatement.addBatch();
                        i++;
                        if( i%100 == 0 ){
                            preparedStatement.executeBatch();
                            logger.info("更新{}条数据",i);
                        }
                    }
                }
                preparedStatement.executeBatch();
                connection.commit();
                logger.info("更新{}条数据",i);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 1.获取待处理的tripIndex数据,每条数据均包含上月区域画像的数据
     * @return
     * @param statTime
     */
    private Map<String,Map<String,TripIndex>> getPendingData(String statTime) {
        if(initMSSQL()){
            Map<String,Map<String,TripIndex>> tripIndexDateMap = new HashMap<>(16);

            String sql = "SELECT a.ID,a.StatDate,a.AreaID,a.OnlineNum,a.OnlineNumExpansion,LocalVehicleNum,MaxOnlineNum,MaxFireNum,LocalVehicleNumExt,MaxOnlineNumExt,MaxFireNumExt FROM rtm_tripIndex a LEFT JOIN IDBox_Master.dbo.stat_month_distPortrait b ON a.AreaID = b.DistID where a.Status = -1 AND b.StatYear = ? and b.StatMonth = ? AND a.StatDate <= ? AND a.StatDate >= DATEADD(MINUTE, -20, ?)";

            try{
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1,year);
                preparedStatement.setInt(2,month);
                preparedStatement.setString(3,statTime);
                preparedStatement.setString(4,statTime);
                ResultSet rs = preparedStatement.executeQuery();
                while(rs.next()){
                    String statDate = rs.getString("StatDate");
                    String areaID = rs.getString("AreaID");

                    TripIndex tripIndex = new TripIndex();
                    tripIndex.setId(rs.getInt("ID"));
                    tripIndex.setStatDate(statDate);
                    tripIndex.setAreaID(areaID);
                    tripIndex.setOnlineNum(rs.getInt("OnlineNum"));
                    tripIndex.setOnlineNumExpansion(rs.getInt("OnlineNumExpansion"));
                    tripIndex.setLastMonthLocalVehicleNum(rs.getInt("LocalVehicleNum"));
                    tripIndex.setLastMonthMaxOnlineVehicleNum(rs.getInt("MaxOnlineNum"));
                    tripIndex.setLastMonthMaxFireNum(rs.getInt("MaxFireNum"));
                    tripIndex.setLastMonthLocalVehicleNumExt(rs.getInt("LocalVehicleNumExt"));
                    tripIndex.setLastMonthMaxOnlineVehicleNumExt(rs.getInt("MaxOnlineNumExt"));
                    tripIndex.setLastMonthMaxFireNumExt(rs.getInt("MaxFireNumExt"));

                    if(tripIndexDateMap.get(statDate) != null){
                        tripIndexDateMap.get(statDate).put(areaID,tripIndex);
                    }else{
                        Map<String,TripIndex> tripIndexAreaMap = new HashMap<>(32);
                        tripIndexAreaMap.put(areaID,tripIndex);
                        tripIndexDateMap.put(statDate,tripIndexAreaMap);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return tripIndexDateMap;
        }
        return null;
    }

    /**
     * 2.获取点火车辆数
     * @param tripIndexAreaMap
     * @param statDate
     */
    private void getFireNum(Map<String, TripIndex> tripIndexAreaMap, String statDate) {
        if(initMSSQL()){
            String sql = "SELECT DistID as AreaID,COUNT (DISTINCT(vid)) as FireNum FROM rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN DATEADD(MINUTE ,-5, ?) AND ? GROUP BY DistID ";
            sql +=  " UNION ALL " +
                    " SELECT RingID as AreaID,COUNT (DISTINCT(vid)) as FireNum FROM rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN DATEADD(MINUTE ,-5, ?) AND ? GROUP BY RingID ";
            sql +=  " UNION ALL " +
                    " SELECT 100 as AreaID,COUNT (DISTINCT(vid)) as FireNum FROM rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN DATEADD(MINUTE ,-5, ?) AND ? AND DistID >=205 and DistID <=210 ";
            sql +=  " UNION ALL " +
                    " SELECT 1000 as AreaID,COUNT (DISTINCT(vid)) as FireNum FROM  rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN DATEADD(MINUTE ,-5, ?) AND ?";
            try{
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,statDate);
                preparedStatement.setString(2,statDate);
                preparedStatement.setString(3,statDate);
                preparedStatement.setString(4,statDate);
                preparedStatement.setString(5,statDate);
                preparedStatement.setString(6,statDate);
                preparedStatement.setString(7,statDate);
                preparedStatement.setString(8,statDate);

                ResultSet rs = preparedStatement.executeQuery();
                while(rs.next()){
                    String areaID = rs.getString("AreaID");
                    TripIndex tripIndex = tripIndexAreaMap.get(areaID);
                    if(tripIndex != null){
                        tripIndex.setFireNum(rs.getInt("FireNum"));
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 3.获取点火车辆扩样后数量
     * @param tripIndexAreaMap
     * @param statDate
     */
    private void getFireNumExpansion(Map<String, TripIndex> tripIndexAreaMap, String statDate) {
        if(initMSSQL()){
            //区域
            String sql = "SELECT c.DistID as AreaID,SUM(c.num) as FireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num,DistID" +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN DATEADD(MINUTE,-5, ?) AND ? AND b.StatYear = ? AND b.StatMonth = ?)c " +
                    " GROUP BY c.DistID";
            //环路
            sql += " UNION ALL " +
                    " SELECT c.RingID as AreaID,SUM(c.num) as FireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num,RingID " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN DATEADD(MINUTE,-5, ?) AND ? AND b.StatYear = ? AND b.StatMonth = ?)c " +
                    " GROUP BY c.RingID";
            //中心城区
            sql += " UNION ALL " +
                    " SELECT 100 as AreaID,SUM(c.num) as FireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.DistID >=205 and a.DistID <=210 AND a.fireTime BETWEEN DATEADD(MINUTE,-5, ?) AND ? AND b.StatYear = ? AND b.StatMonth = ?)c ";
            //全市
            sql += " UNION ALL " +
                    " SELECT 1000 as AreaID,SUM(c.num) as FireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN DATEADD(MINUTE,-5, ?) AND ? AND b.StatYear = ? AND b.StatMonth = ?)c ";
            try{
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,statDate);
                preparedStatement.setString(2,statDate);
                preparedStatement.setInt(3,year);
                preparedStatement.setInt(4,month);
                preparedStatement.setString(5,statDate);
                preparedStatement.setString(6,statDate);
                preparedStatement.setInt(7,year);
                preparedStatement.setInt(8,month);
                preparedStatement.setString(9,statDate);
                preparedStatement.setString(10,statDate);
                preparedStatement.setInt(11,year);
                preparedStatement.setInt(12,month);
                preparedStatement.setString(13,statDate);
                preparedStatement.setString(14,statDate);
                preparedStatement.setInt(15,year);
                preparedStatement.setInt(16,month);

                ResultSet rs = preparedStatement.executeQuery();
                while(rs.next()){
                    String areaID = rs.getString("AreaID");
                    TripIndex tripIndex = tripIndexAreaMap.get(areaID);
                    if(tripIndex != null){
                        tripIndex.setFireNumExpansion(rs.getInt("FireNumExt"));
                    }
                }

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 4.获取累计点火车辆数
     * @param tripIndexAreaMap
     * @param statDate
     */
    private void getSumFireNum(Map<String, TripIndex> tripIndexAreaMap, String statDate) {
        if(initMSSQL()){
            //区域
            String sql = "SELECT DistID as AreaID,COUNT (DISTINCT(vid)) as SumFireNum FROM rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN ? AND ? GROUP BY DistID ";
            //环路
            sql +=  " UNION ALL " +
                    " SELECT RingID as AreaID,COUNT (DISTINCT(vid)) as SumFireNum FROM rtm_fireVehicle " +
                    " WHERE fireStatus = 1 AND fireTime BETWEEN ? AND ? GROUP BY RingID ";
            //中心城
            sql +=  " UNION ALL " +
                    " SELECT 100 as AreaID,COUNT (DISTINCT(vid)) as SumFireNum FROM rtm_fireVehicle WHERE fireStatus = 1 AND fireTime BETWEEN ? AND ? AND DistID >=205 and DistID <=210 ";
            //全市
            sql +=  " UNION ALL " +
                    " SELECT 1000 as AreaID,COUNT (DISTINCT(vid)) as SumFireNum FROM  rtm_fireVehicle WHERE fireStatus = 1 AND fireTime BETWEEN ? AND ?";
            try{
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,statDate.substring(0,10));
                preparedStatement.setString(2,statDate);
                preparedStatement.setString(3,statDate.substring(0,10));
                preparedStatement.setString(4,statDate);
                preparedStatement.setString(5,statDate.substring(0,10));
                preparedStatement.setString(6,statDate);
                preparedStatement.setString(7,statDate.substring(0,10));
                preparedStatement.setString(8,statDate);

                ResultSet rs = preparedStatement.executeQuery();
                while(rs.next()){
                    String areaID = rs.getString("AreaID");
                    TripIndex tripIndex = tripIndexAreaMap.get(areaID);
                    if(tripIndex != null){
                        tripIndex.setSumFireNum(rs.getInt("SumFireNum"));
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 5.获取累计点火扩样后车辆数
     * @param tripIndexAreaMap
     * @param statDate
     */
    private void getSumFireNumExpansion(Map<String, TripIndex> tripIndexAreaMap, String statDate) {
        if(initMSSQL()){
            //区域
            String sql = "SELECT c.DistID as AreaID,SUM(c.num) as SumFireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num,DistID " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN ? AND ? AND b.StatYear = ? AND b.StatMonth = ?)c " +
                    " GROUP BY c.DistID";
            //环路
            sql += " UNION ALL " +
                    " SELECT c.RingID as AreaID,SUM(c.num) as SumFireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num,RingID " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN ? AND ? AND b.StatYear = ? AND b.StatMonth = ?)c " +
                    " GROUP BY c.RingID";
            //中心城区
            sql += " UNION ALL " +
                    " SELECT 100 as AreaID,SUM(c.num) as SumFireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.DistID >=205 AND a.DistID <=210 AND a.fireTime BETWEEN ? AND ? AND b.StatYear = ? AND b.StatMonth = ?)c ";
            //全市
            sql += " UNION ALL " +
                    " SELECT 1000 as AreaID,SUM(c.num) as SumFireNumExt FROM " +
                    "(SELECT DISTINCT a.VID,b.BoxWeight num " +
                    " FROM rtm_fireVehicle a " +
                    " LEFT JOIN [IDBox_Master].[dbo].[stat_month_userPortrait] b ON a.VID = b.vid " +
                    " WHERE  a.fireStatus = 1 AND a.fireTime BETWEEN ? AND ? AND b.StatYear = ? AND b.StatMonth = ?)c ";
            try{
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,statDate.substring(0,10));
                preparedStatement.setString(2,statDate);
                preparedStatement.setInt(3,year);
                preparedStatement.setInt(4,month);
                preparedStatement.setString(5,statDate.substring(0,10));
                preparedStatement.setString(6,statDate);
                preparedStatement.setInt(7,year);
                preparedStatement.setInt(8,month);
                preparedStatement.setString(9,statDate.substring(0,10));
                preparedStatement.setString(10,statDate);
                preparedStatement.setInt(11,year);
                preparedStatement.setInt(12,month);
                preparedStatement.setString(13,statDate.substring(0,10));
                preparedStatement.setString(14,statDate);
                preparedStatement.setInt(15,year);
                preparedStatement.setInt(16,month);

                ResultSet rs = preparedStatement.executeQuery();
                while(rs.next()){
                    String areaID = rs.getString("AreaID");
                    TripIndex tripIndex = tripIndexAreaMap.get(areaID);
                    if(tripIndex != null){
                        tripIndex.setSumFireNumExpansion(rs.getInt("SumFireNumExt"));
                    }
                }

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 6.通过已有的字段值，计算出其他字段
     * @param tripIndexAreaMap
     */
    private void calculateOtherField(Map<String, TripIndex> tripIndexAreaMap) {
        for(String areaID : tripIndexAreaMap.keySet()){
            TripIndex tripIndex = tripIndexAreaMap.get(areaID);
            //熄火车辆
            int unfireNum = 0;
            if(tripIndex.getLastMonthLocalVehicleNum() != null){
                if(tripIndex.getFireNum() != null){
                    unfireNum = tripIndex.getLastMonthLocalVehicleNum()-tripIndex.getFireNum();
                }else{
                    unfireNum = tripIndex.getLastMonthLocalVehicleNum();
                }
            }
            tripIndex.setUnfireNum(unfireNum);

            int unfireNumExpansion = 0;
            if(tripIndex.getLastMonthLocalVehicleNumExt() != null){
                if(tripIndex.getFireNumExpansion() != null){
                    unfireNumExpansion =tripIndex.getLastMonthLocalVehicleNumExt() - tripIndex.getFireNumExpansion();
                }else{
                    unfireNumExpansion =tripIndex.getLastMonthLocalVehicleNumExt();
                }
            }
            tripIndex.setUnfireNumExpansion(unfireNumExpansion);

            //离线车辆
            int offlineNum =0;
            if(tripIndex.getLastMonthLocalVehicleNum() != null){
                if(tripIndex.getOnlineNum() != null){
                    offlineNum = tripIndex.getLastMonthLocalVehicleNum() - tripIndex.getOnlineNum();
                }else{
                    offlineNum = tripIndex.getLastMonthLocalVehicleNum();
                }
            }
            int offlineNumExpansion = 0;
            if(tripIndex.getLastMonthLocalVehicleNumExt() != null){
                if(tripIndex.getOnlineNumExpansion() != null){
                    offlineNumExpansion = tripIndex.getLastMonthLocalVehicleNumExt() - tripIndex.getOnlineNumExpansion();
                }else{
                    offlineNumExpansion = tripIndex.getLastMonthLocalVehicleNumExt();
                }
            }
            tripIndex.setOfflineNum(offlineNum);
            tripIndex.setOfflineNumExpansion(offlineNumExpansion);

            //出车率
            double outRate = 0.0;
            if(tripIndex.getSumFireNum() != null && tripIndex.getLastMonthLocalVehicleNum() != null){
                outRate = tripIndex.getSumFireNum() * 1.0 / tripIndex.getLastMonthLocalVehicleNum();
             }
            double outRateExpansion = 0;
            if(tripIndex.getSumFireNumExpansion() != null && tripIndex.getLastMonthLocalVehicleNumExt() != null){
                outRateExpansion = tripIndex.getSumFireNumExpansion() * 1.0 / tripIndex.getLastMonthLocalVehicleNumExt();
            }
            tripIndex.setOutRate(outRate);
            tripIndex.setOutRateExpansion(outRateExpansion);

            //点火系数
            double fireIndex = 0;
            if(tripIndex.getFireNum() != null && tripIndex.getLastMonthMaxFireNum() != null){
                fireIndex = tripIndex.getFireNum() * 1.0/ tripIndex.getLastMonthMaxFireNum();
            }
            double fireIndexExpansion = 0;
            if(tripIndex.getFireNumExpansion() != null  && tripIndex.getLastMonthMaxFireNumExt() != null){
                fireIndexExpansion = tripIndex.getFireNumExpansion() * 1.0/ tripIndex.getLastMonthMaxFireNumExt();
            }
            tripIndex.setFireIndex(fireIndex);
            tripIndex.setFireIndexExpansion(fireIndexExpansion);

            //压力系数
            double pressIndex = 0;
            if(tripIndex.getOnlineNum() != null && tripIndex.getLastMonthMaxOnlineVehicleNum() != null){
                pressIndex = tripIndex.getOnlineNum() * 1.0 / tripIndex.getLastMonthMaxOnlineVehicleNum();
            }
            double pressIndexExpansion = 0;
            if(tripIndex.getOnlineNumExpansion() != null && tripIndex.getLastMonthMaxOnlineVehicleNumExt() != null){
                pressIndexExpansion = tripIndex.getOnlineNumExpansion() *1.0 / tripIndex.getLastMonthMaxOnlineVehicleNumExt();
            }
            tripIndex.setPressIndex(pressIndex);
            tripIndex.setPressIndexExpansion(pressIndexExpansion);
        }
    }
}
